package com.inifitness.admin.core.system.controller;

import java.util.List;

import jakarta.validation.Valid;

import com.inifitness.admin.core.config.resubmit.annotation.PreventDuplicateSubmit;
import com.inifitness.admin.core.system.model.form.DeptForm;
import com.inifitness.admin.core.system.model.query.DeptQuery;
import com.inifitness.admin.core.system.model.vo.DeptVO;
import com.inifitness.admin.core.system.service.SysDeptService;
import com.inifitness.common.model.Option;
import com.inifitness.common.result.Result;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 部门控制器
 *
 * @author sunjinfeng
 * @since 2020/11/6
 */
@Tag(name = "05.部门接口")
@RestController
@RequestMapping("/api/v1/dept")
@RequiredArgsConstructor
public class SysDeptController {

  private final SysDeptService deptService;

  @Operation(summary = "获取部门列表")
  @GetMapping
  public Result<List<DeptVO>> listDepartments(
      @ParameterObject DeptQuery queryParams
  ) {
    List<DeptVO> list = deptService.listDepartments(queryParams);
    return Result.success(list);
  }

  @Operation(summary = "获取部门下拉选项")
  @GetMapping("/options")
  public Result<List<Option>> listDeptOptions() {
    List<Option> list = deptService.listDeptOptions();
    return Result.success(list);
  }

  @Operation(summary = "获取部门表单数据")
  @GetMapping("/{deptId}/form")
  public Result<DeptForm> getDeptForm(
      @Parameter(description = "部门ID") @PathVariable(value = "deptId") Long deptId
  ) {
    DeptForm deptForm = deptService.getDeptForm(deptId);
    return Result.success(deptForm);
  }

  @Operation(summary = "新增部门")
  @PostMapping
  @PreAuthorize("@ss.hasPerm('sys:dept:add')")
  @PreventDuplicateSubmit
  public Result saveDept(
      @Valid @RequestBody DeptForm formData
  ) {
    Long id = deptService.saveDept(formData);
    return Result.success(id);
  }

  @Operation(summary = "修改部门")
  @PutMapping(value = "/{deptId}")
  @PreAuthorize("@ss.hasPerm('sys:dept:edit')")
  public Result updateDept(
      @PathVariable("deptId") Long deptId,
      @Valid @RequestBody DeptForm formData
  ) {
    Long newDeptId = deptService.updateDept(deptId, formData);
    return Result.success(newDeptId);
  }

  @Operation(summary = "删除部门")
  @DeleteMapping("/{ids}")
  @PreAuthorize("@ss.hasPerm('sys:dept:delete')")
  public Result deleteDepartments(
      @Parameter(description = "部门ID，多个以英文逗号(,)分割") @PathVariable("ids") String ids
  ) {
    boolean result = deptService.deleteByIds(ids);
    return Result.judge(result);
  }

}
